//
//  FeedBackViewController.m
//  chanyou
//
//  Created by wanli on 15/1/8.
//  Copyright (c) 2015年 wanli. All rights reserved.
//

#import "FeedBackViewController.h"
//#import "ViewController.h"
//#import "SMTPSenderAppDelegate.h"
#import "SKPSMTPMessage.h"
#import "NSData+Base64Additions.h"
@interface FeedBackViewController ()<UITextViewDelegate, UITextFieldDelegate>

@end

@implementation FeedBackViewController
- (void)dealloc
{
//    [_lab release];
//    [_content release];
//    [_email release];
//    [_content_Image release];
//    [_email_Image release];
//    [_send release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"login_backgroud_biaomian"] forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(backAction)];
    
    UIImage *img = [UIImage imageNamed:@"user-header-view.jpg"];
    UIImageView *background = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    background.image = img;
    [self.view addSubview:background];
//    [background release];

    self.send = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"feedBack.png"] style:UIBarButtonItemStyleDone target:self action:@selector(sendMessage:)];
    
    self.navigationItem.title = @"反馈意见";
    
    
    self.content = [[UITextView alloc] initWithFrame:CGRectMake(10, 80, self.view.frame.size.width - 20, 100)];
    _content.font = [UIFont systemFontOfSize:20];
    _content.textColor = [UIColor blueColor];

    [self.view addSubview:_content];
    [_content becomeFirstResponder];
    _content.delegate = self;
//    [_content release];
    

    self.lab = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 30)];
    _lab.font = [UIFont systemFontOfSize:20];
    _lab.text = @"给点意见";
    _lab.enabled = NO;
    [_content addSubview:_lab];
//    [_lab release];
    
    self.content_Image = [[UIImageView alloc] initWithFrame:CGRectMake(5, _content.frame.size.height + 75, _content.frame.size.width + 10, 20)];
//    diImage.backgroundColor = [UIColor redColor];
    _content_Image.image = [UIImage imageNamed:@"di_lan.png"];
    [self.view addSubview:_content_Image];
//    [_content_Image release];
    
    self.email_Image = [[UIImageView alloc] initWithFrame:CGRectMake(5, _content.frame.size.height + 160, _content.frame.size.width + 10, 20)];
    //    diImage.backgroundColor = [UIColor redColor];
    _email_Image.image = [UIImage imageNamed:@"di_hui.png"];
    [self.view addSubview:_email_Image];
//    [_email_Image release];
    
    
    self.email = [[UITextField alloc] initWithFrame:CGRectMake(15, 220, self.view.frame.size.width - 20, 50)];
//    _email.borderStyle = UITextBorderStyleRoundedRect;
    _email.placeholder = @"Email/QQ";
    _email.delegate = self;
//    _email.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:_email];
//    [_email release];
}

//实现UITextView的代理方法,当输入框有文字的时候,将提示内容删除,如果输入框没有内容,则提示输入.
- (void)textViewDidChange:(UITextView *)textView{
    if (_content.text.length == 0) {
        _lab.text = @"给点意见";
     
    }else{
        _lab.text = @"";
        self.navigationItem.rightBarButtonItem = _send;
    }
  
}
- (void)textViewDidEndEditing:(UITextView *)textView{
//    _content.backgroundColor = [UIColor greenColor];
    _content_Image.image = [UIImage imageNamed:@"di_hui.png"];
    [self.view addSubview:_content_Image];
    
}
- (void)textViewDidBeginEditing:(UITextView *)textView{
//        _content.backgroundColor = [UIColor orangeColor];
    _content_Image.image = [UIImage imageNamed:@"di_lan.png"];
    [self.view addSubview:_content_Image];
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
//    _email.backgroundColor = [UIColor greenColor];
    _email_Image.image = [UIImage imageNamed:@"di_hui.png"];
    [self.view addSubview:_email_Image];
    
}


- (void)textFieldDidBeginEditing:(UITextField *)textField{
//    _email.backgroundColor = [UIColor orangeColor];
    _email_Image.image = [UIImage imageNamed:@"di_lan.png"];
    [self.view addSubview:_email_Image];
}
//
- (BOOL)canBecomeFirstResponder{
    return YES;
}

- (void)backAction{
    [self dismissViewControllerAnimated:YES completion:nil];
}



- (void)sendMessage:(UIButton *)sender {
    
    //    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    sender.enabled = NO;
    SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
    testMsg.fromEmail = @"wanlicwl@126.com";
    
    testMsg.toEmail = @"wanlicwl@qq.com";
    //    testMsg.bccEmail = [defaults objectForKey:@"bccEmal"];
    testMsg.relayHost = @"smtp.126.com";
    
    //    testMsg.requiresAuth = [[defaults objectForKey:@"requiresAuth"] boolValue];
    testMsg.requiresAuth = YES;
    
    if (testMsg.requiresAuth) {
        testMsg.login = @"wanlicwl@126.com";
        
        testMsg.pass = @"wanlicwl@yyy";
        
    }
    
    testMsg.wantsSecure = YES;
    //    testMsg.wantsSecure = [[defaults objectForKey:@"wantsSecure"] boolValue]; // smtp.gmail.com doesn't work without TLS!
    
    NSString *subject = [NSString stringWithFormat:@"%@ 的反馈意见", _email.text];
    testMsg.subject = subject;
    //testMsg.bccEmail = @"testbcc@test.com";
    
    // Only do this for self-signed certs!
    // testMsg.validateSSLChain = NO;
    testMsg.delegate = self;
    
    NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain; charset=UTF-8",kSKPSMTPPartContentTypeKey,
                               _content.text,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
    
    //    NSString *vcfPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"vcf"];
    //    NSData *vcfData = [NSData dataWithContentsOfFile:vcfPath];
    //
    //    NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"test.vcf\"",kSKPSMTPPartContentTypeKey,
    //                             @"attachment;\r\n\tfilename=\"test.vcf\"",kSKPSMTPPartContentDispositionKey,[vcfData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];
    
    testMsg.parts = [NSArray arrayWithObjects:plainPart,nil];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [testMsg send];
    });
}
- (void)messageSent:(SKPSMTPMessage *)message
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"发送成功, 谢谢你的反馈" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
//    [alert release];
//    [message release];
    [self backAction];
    //    self.textView.text  = @"Yay! Message was sent!";
    //NSLog(@"delegate - message sent");
}

- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"发送失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
//    [alert release];
   
//    [message release];
    

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_content resignFirstResponder];
    [_email resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
